/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjetoCompreJa;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 *
 */
public class Connect_BD_CJ {

    Statement stmt;
    Connection conn;

    //**
    public Statement conectar() throws ClassNotFoundException, SQLException {
        //return this.conectarPostGreSql();
        return this.conectarMySql();
    }

    public void desconectar() throws SQLException {
        conn.close();
    }

    private Statement conectarMySql() throws ClassNotFoundException, SQLException {
        try {

            String driver = "com.mysql.jdbc.Driver";
            String dataBaseName = "teste";
            String url = "jdbc:mysql://localhost:3306/";
            String usuario = "root";
            String senha = "xxxx";
            Class.forName(driver).newInstance();
            conn = (Connection) DriverManager.getConnection(url + dataBaseName, usuario, senha);
            stmt = conn.createStatement();
            return stmt;
        } catch (InstantiationException ex) {
            throw new SQLException(ex.getMessage());
        } catch (IllegalAccessException ex) {
            throw new SQLException(ex.getMessage());
        }
    }

    /*       private Statement conectarPostGreSql() throws ClassNotFoundException, SQLException {
     * Class.forName("org.postgresql.Driver");
     * String local = "localhost:5432";
     * String banco = "ProjetoX";
     * String usuario = "postgres";
     * String senha = "xxx";
     * conn = DriverManager.getConnection("jdbc:postgresql://"
     * + local + "/" + banco
     * + "?charSet=LATIN1", usuario,
     * senha);
     * stmt = conn.createStatement();
     * //return conn.createStatement();
     * return stmt;
     * }*/
}
